
package cn.edu.nuc.creditSystem.service;

import java.util.ArrayList;
import java.util.List;

import cn.edu.nuc.creditSystem.bean.BaseBean;
import cn.edu.nuc.creditSystem.bean.TreeBean;
import cn.edu.nuc.creditSystem.dao.BaseMapper;
import cn.edu.nuc.creditSystem.utils.PageBean;

public abstract class BaseImpl<T extends BaseBean> implements BaseService<T> {

	public abstract BaseMapper<T> getBaseDao();
	
	public int insert(T t) {
		return  getBaseDao().insert(t);
	}

	public T get(Integer id) {
		return getBaseDao().get(id);
	}

	public int update(T t) {
		return getBaseDao().update(t);
	}

	public int count(T query) {
		return getBaseDao().countAll(query);
	}

	public List<T> query(T query, PageBean page) {
		if(page != null) {
			
			int total = getBaseDao().countAll(query);
			page.setTotal(total);
		}
		List<T> list = getBaseDao().query(query, page);
		return list;
	}

	public List<T> getAll(T query) {
		return getBaseDao().findAll(query);
	}

	public int delete(int id) {
		getBaseDao().delete(id);
		return 0;
	}
	
	
	public int updateStatus(String ids, int status){
		return 	getBaseDao().updateStatus(ids, status);
	}
	public List<TreeBean> queryTree(String param){
		List<TreeBean> list=getBaseDao().queryTree(param);
		if(list!=null && !list.isEmpty()){
			List<TreeBean> treeList = new ArrayList<TreeBean>();
			TreeBean ptree = null;
			Integer pid=-1;
			for(TreeBean t:list){
				if(pid==-1 || pid!=t.getPid()){
					if(ptree!=null)treeList.add(ptree);
					ptree=new TreeBean(t.getPid(),t.getPtext());
					ptree.setChildren(new ArrayList<TreeBean>());
					ptree.getChildren().add(new TreeBean(t.getId(),t.getText()));
					pid=t.getPid();
				}else{
					ptree.getChildren().add(new TreeBean(t.getId(),t.getText()));
				}
			}
			if(ptree!=null)treeList.add(ptree);
			return treeList;
		}else{
			return null;
		}
	}
}
